/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.mes.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import me.zhengjie.modules.mes.domain.MesTemplateConfig;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.mes.service.MesTemplateConfigService;
import me.zhengjie.modules.mes.domain.vo.MesTemplateConfigQueryCriteria;
import me.zhengjie.modules.mes.mapper.MesTemplateConfigMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author mfc
* @date 2025-01-02
**/
@Service
@RequiredArgsConstructor
public class MesTemplateConfigServiceImpl extends ServiceImpl<MesTemplateConfigMapper, MesTemplateConfig> implements MesTemplateConfigService {

    private final MesTemplateConfigMapper mesTemplateConfigMapper;

    @Override
    public PageResult<MesTemplateConfig> queryAll(MesTemplateConfigQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(mesTemplateConfigMapper.findAll(criteria, page));
    }

    @Override
    public List<MesTemplateConfig> queryAll(MesTemplateConfigQueryCriteria criteria){
        return mesTemplateConfigMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(MesTemplateConfig resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(MesTemplateConfig resources) {
        MesTemplateConfig mesTemplateConfig = getById(resources.getId());
        mesTemplateConfig.copy(resources);
        saveOrUpdate(mesTemplateConfig);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<MesTemplateConfig> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (MesTemplateConfig mesTemplateConfig : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("组件名称", mesTemplateConfig.getComponentName());
            map.put("标签名称", mesTemplateConfig.getTagLabel());
            map.put("标签key", mesTemplateConfig.getTagKey());
            map.put("模版名称", mesTemplateConfig.getTemplatePage());
            map.put("模版名称_en", mesTemplateConfig.getTemplatePageEn());
            map.put("组件名称_en", mesTemplateConfig.getComponentNameEn());
            map.put("控件名称", mesTemplateConfig.getControlName());
            map.put("控件名称_en", mesTemplateConfig.getControlNameEn());
            map.put("排序_key", mesTemplateConfig.getSortKey());
            map.put("排序", mesTemplateConfig.getSort());
            map.put("内容控件名称_key", mesTemplateConfig.getContentControlKey());
            map.put("内容控件", mesTemplateConfig.getContentControl());
            map.put("内容颜色_key", mesTemplateConfig.getContentColorKey());
            map.put("内容颜色", mesTemplateConfig.getContentColor());
            map.put("关联表", mesTemplateConfig.getFromTable());
            map.put("关联字段", mesTemplateConfig.getFromField());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public void deleteByfunctionId(int targetFunctionId1, int targetFunctionId2, int targetFunctionId3) {
        mesTemplateConfigMapper.delete(new QueryWrapper<MesTemplateConfig>().eq("target_function_id1", targetFunctionId1)
                .eq("target_function_id2", targetFunctionId2)
                .eq("target_function_id3", targetFunctionId3));

    }
}